package jdbc.datasource;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

//基于druid数据库连接池的工具类
public class JDBCUtilsByDruid {
    private static DataSource dataSource;
    //在静态代码块完成 ds初始化
    static{
        try {
            Properties properties = new Properties();
            properties.load(new FileInputStream("src/druid.properties"));
             dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //编写getConnection方法
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }
    //关闭连接,在数据库连接池技术中，close 不是真的断掉连接
    //而是把使用的Connection对象放回连接池
    public static void close(ResultSet resultSet, Connection connection, Statement statement){
        try {
            if(resultSet != null){
                resultSet.close();
            }
            if(connection != null){
                connection.close();
            }
            if(statement != null){
                statement.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
